import captcha from '@dfeidao/atom-nodejs/captcha';
import msg from '@dfeidao/atom-nodejs/msg';
import { IncomingHttpHeaders } from 'http';
interface Message {
	cookie: {
		uk: string;
		[key: string]: string
	};
	urls: {
		base: string;
		origin: string;
		url: string;
	};
	query: {};
	params: {};
	headers: { cookie: string };
	captcha: string;
	usercode: string;
	password: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	const res = await captcha.check(message.cookie, message.captcha);
	if (res) {
		const result = await msg.user_login(action_id, session_id, message.usercode, message.password);
		return {
			data: result,
			cookie: {
				sessionid: result.sessionID
			}
		};
	} else {
		throw new Error('验证码错误,请您重新输入');
	}

}
